#!/bin/bash
#***************************************************************************************************************
# FILE: Test.sh
# 
# DESCRIPTION
# Tests the singly-linked list code.
# 
# AUTHOR INFORMATION
# Kevin R. Burger [KRB]
# 
# Mailing Address:
# Computer Science & Engineering
# School of Computing, Informatics, and Decision Systems Engineering
# Arizona State University
# Tempe, AZ 85287-8809
# 
# Email: burgerk@asu
# Web:   http://kevin.floorsoup.com
# 
# MODIFICATION HISTORY:
# -------------------------------------------------------------------------------------------------------------
# 29 Feb 2012 [KRB] Initial revision.
#***************************************************************************************************************

#----- Dos2Unix ------------------------------------------------------------------------------------------------
# Converts a text file stored in MSDOS/Windows format to Unix format. In MSDOS/Windows, each line of a
# text file is terminated with a carriage return (CR)/linefeed (LF) sequence. In Unix, each line of a 
# text file is simply terminated with a LF. The dos2unix command simply replaces each occurrence of CR/LF
# with LF.
#
# BTW, this is necessary because I edit my files on my PC using a Windows text editor. Then I transfer the
# files up to General using WinSCP, and I do my building, debugging, and testing on General. If I were simply
# using VI on General, then this function would not be necessary.
#---------------------------------------------------------------------------------------------------------------
#Dos2Unix() {
#	dos2unix < $_file > $_file.new
#	rm $_file
#	mv $_file.new $_file
#}

#----- Test ----------------------------------------------------------------------------------------------------
# Perform the test case corresponding to the value of variable _tc.
#---------------------------------------------------------------------------------------------------------------
Test() {
	echo -n Performing Test Case $_tc...

	# Set _correct to the name of the file containing the correct output from the test case. Set _diff to the
	# text filename that will be generated by the diff command. Set _in to the name of text file containing the
	# input data for the test case. Set _out to the name of the output file that will be generated by perform-
	# ing the test case.
	_correct=test$_tc.correct
	_diff=test$_tc.diff
	_in=test$_tc.in
	_out=test$_tc.out

	# Convert the test case input file to Unix format.
	_file=$_in
    #Dos2Unix

	# Convert the test case 'correct' file to Unix format.
	_file=$_correct
    #Dos2Unix

	# Perform the test.
	$_binary $_in $_out

	# Convert the test case output file to Unix format, just in case.
	_file=$_out
    #Dos2Unix

	# Perform the diff command on the test case output file and the test case 'correct' file. If there are
	# any differences then the .diff file will be nonempty, but if there are no differences, then the .diff
	# file will not exist. The 'test -s' command tests to see if the .diff file exists. If it does, then
	# the test case failed. If the .diff file does not exist, then the test case passed.
	diff $_out $_correct > $_diff

	if test -s $_diff; then
		echo "FAILED. See" $_out "for output."
	else
		rm -f $_diff
		rm -f $_out
		echo "PASSED"
	fi
}

# _binary is the name of the executable program (i.e., in Unix lingo this is called a binary).
_binary=./ListTest

# Store the current working directory in _currdir. We are going to cd to the testing directory and
# when we are done testing, we will cd back to the current directory.
_currdir=`pwd`

# This is the directory containing the test cases.
_testdir=testcases

# Copy the binary to the test cases directory.
cp $_binary $_testdir

# cd to the test cases directory.
cd $_testdir

# Let _tc take on the values 1, 2, 3, ..., 13. The test case files are named test1.in, test2.in,
# ..., test13.in. For each value of _tc, call the Test function.
for _tc in `seq 1 36`; do
	Test
done

# cd back to the original working directory.
cd $_curdir

# Delete all variables.
_binary=
_curdir=
_correct=
_file=
_in=
_out=
_tc=
_testdir=
